//
//  TextEditViewController.m
//  笔记1
//
//  Created by  on 2022/11/5.
//

#import "TextEditViewController.h"
#import "ColorCell.h"

@interface TextEditViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UILabel *min;
@property (strong, nonatomic) IBOutlet UILabel *max;
@property (strong, nonatomic) NSArray *array;
@property (assign, nonatomic) NSInteger index;

@end

@implementation TextEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = @[@"#FFFFFF",@"#010101",@"#0533A1",@"#303030",@"#CD3F30",@"#FADFEE",@"#DFD2F9",@"#D5F9E9",@"#FEF5BE"];
    [self.collectionView registerNib:[ColorCell nib] forCellWithReuseIdentifier:[ColorCell ID]];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (IBAction)actionForSlider:(UISlider *)sender {
    if (self.fontBlock) {
        self.fontBlock(sender.value);
    }
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ColorCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ColorCell ID] forIndexPath:indexPath];
    NSString *color = self.array[indexPath.row];
    cell.colorView.bgHexColor = color;
    cell.cornerRadius = 20;
    cell.borderWidth = 5;
    cell.borderHexColor = self.index == indexPath.row ? color : @"#F8D1B4";
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.colorBlock) {
        self.index = indexPath.row;
        NSString *color = self.array[indexPath.row];
        self.colorBlock(color);
    }
    [collectionView reloadData];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(40, 40);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(10, 20, 10, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 16;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end

